<template>
  <div class="content">
    <div class="content__list">
      <div
        :class="{
          list__con: true,
          'list__con--active': currentTab === item.tab,
        }"
        v-for="item in classifyList"
        :key="item.name"
        @click="() => handleTabClick(item.tab)"
      >
        {{ item.name }}
      </div>
    </div>
    <div class="product">
      <div v-for="item in list" class="product__item" :key="item._id">
        <img class="product__item__img" :src="item.imgUrl" alt="" />
        <div class="product__item__desc">
          <h4 class="product__title">{{ item.name }}</h4>
          <p class="product__sales">月售{{ item.sales }}件</p>
          <div class="product__price">
            <span class="current__price">￥{{ item.price }}</span>
            <span class="past__price">￥{{ item.oldPrice }}</span>
          </div>
        </div>
        <div class="product__number">
          <span
            class="product__number__minus"
            @click="
              () => {
                changeCartItem(shopId, item._id, item, -1, shopName);
              }
            "
            >-</span
          >
          <span class="num">
            {{ cartList?.[shopId]?.productList?.[item._id]?.count || 0 }}
          </span>
          <span
            class="product__number__plus"
            @click="
              () => {
                changeCartItem(shopId, item._id, item, 1, shopName);
              }
            "
            >+</span
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs, ref, watchEffect } from "vue";
import { get } from "../../utils/request";
import { useRoute } from "vue-router";
import { useCommonCartEffect } from "../../effect/cartEffects";
import { useStore } from "vuex";
const classifyList = [
  {
    name: "全部商品",
    tab: "all",
  },
  {
    name: "秒杀",
    tab: "seckill",
  },
  {
    name: "新鲜水果",
    tab: "fruit",
  },
];
// 和tab切换有关的逻辑
const useTabEffect = () => {
  const currentTab = ref(classifyList[0].tab);
  const handleTabClick = (tab) => {
    currentTab.value = tab;
  };
  return { currentTab, handleTabClick };
};
// 商品详情信息相关的内容
const useContentListEffect = (currentTab, shopId) => {
  const content = reactive({ list: [] });
  const getContentData = async () => {
    const result = await get(`/api/shop/${shopId}/products`, {
      tab: currentTab.value,
    });
    if (result.errno === 0) {
      content.list = result.data;
    }
  };
  watchEffect(() => {
    getContentData();
  });
  const { list } = toRefs(content);
  return { list };
};
// 与购物车有关的操作
const useCartEffect = () => {
  const store = useStore();
  const { cartList, changeItemToCart } = useCommonCartEffect();
  const changeShopName = (shopId, shopName) => {
    store.commit("changeShopName", { shopId, shopName });
  };
  const changeCartItem = (shopId, productId, item, num, shopName) => {
    changeItemToCart(shopId, productId, item, num);
    changeShopName(shopId, shopName);
  };
  return { cartList, changeCartItem, changeShopName };
};
export default {
  name: "Content",
  props: ["shopName"],
  setup() {
    const route = useRoute();
    const shopId = route.params.id;
    const { currentTab, handleTabClick } = useTabEffect();
    const { list } = useContentListEffect(currentTab, shopId);
    const { cartList, changeCartItem, changeShopName } = useCartEffect();
    return {
      list,
      currentTab,
      handleTabClick,
      classifyList,
      shopId,
      cartList,
      changeCartItem,
      changeShopName,
    };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
@import "../../style/mixins.scss";
.content {
  display: flex;
  position: absolute;
  top: 1.7rem;
  left: 0;
  right: 0.18rem;
  bottom: 0.5rem;
  &__list {
    overflow-y: scroll;
    width: 0.76rem;
    height: 100%;
    background-color: $ibput-bgcolor;
    .list__con {
      line-height: 0.4rem;
      text-align: center;
      font-size: 0.14rem;
      color: $content-fontcolor;
    }
  }
  .product {
    overflow-y: scroll;
    flex: 1;
    margin-left: 0.16rem;
    &__item {
      position: relative;
      display: flex;
      padding-bottom: 0.12rem;
      border-bottom: 0.01rem solid #f1f1f1;
      &__img {
        width: 0.68rem;
        height: 0.68rem;
      }
      &__desc {
        overflow: hidden;
        flex: 1;
        margin-left: 0.16rem;
        .product__title {
          margin: 0;
          line-height: 0.2rem;
          font-size: 0.14rem;
          @include ellipsis;
        }
        .product__sales {
          margin: 0.06rem 0;
          line-height: 0.16rem;
          font-size: 0.12rem;
          color: $content-fontcolor;
        }
        .product__price {
          margin: 0;
          line-height: 0.2rem;
          .current__price {
            font-size: 0.14rem;
            color: #e93b3b;
          }
          .past__price {
            margin-left: 0.06rem;
            font-size: 0.12rem;
            color: #999;
            text-decoration: line-through;
          }
        }
      }
      .product__number {
        position: absolute;
        right: 0;
        bottom: 0.12rem;
        &__minus,
        &__plus {
          display: inline-block;
          width: 0.2rem;
          height: 0.2rem;
          text-align: center;
          line-height: 0.16rem;
          font-size: 0.2rem;
          border-radius: 0.1rem;
        }
        &__minus {
          box-sizing: border-box;
          color: $border-color;
          border: 0.01rem solid $border-color;
        }
        &__plus {
          color: #fff;
          background-color: #0091ff;
        }
        .num {
          margin: 0 0.1rem;
          font-size: 0.16rem;
        }
      }
    }
  }
}
.list__con--active {
  background-color: #fff;
}
</style>
